package cn.jly.flink.quickstart;

import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * flatMapFunction
 *
 * line -> (word, 1)
 *
 * @author lanyangji
 * @create 2020-08-14 18:02
 */
public class LineSplitMapFunction implements FlatMapFunction<String, Tuple2<String, Integer>> {
    private final String prefix;
    public LineSplitMapFunction(String pre) {
        this.prefix = pre;
    }

    @Override
    public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
        String[] split = s.split("\\W+");
        for (String s1 : split) {
            if (StringUtils.isNotEmpty(s1.trim())) {
                collector.collect(Tuple2.of(prefix + s1.trim(), 1));
            }
        }
    }
}
